﻿using System;

namespace Microsoft.CSA.FeedSync.FeedSyncLibrarySL
{
    public class FeedSyncItem : Microsoft.CSA.FeedSync.FeedSyncLibrarySL.IFeedSyncItem
    {
        protected System.Xml.Linq.XElement m_XElement;

        protected Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Sync m_Sync;

        public FeedSyncItem()
        {
        }

        public FeedSyncItem(System.Xml.Linq.XElement i_XElement)
        {
            this.Initialize(i_XElement);
        }

        public System.Xml.Linq.XElement XElement
        {
            get
            {
                return m_XElement;
            }
        }

        public virtual void Update(System.DateTime? i_WhenDateTime, string i_By)
        {
            m_Sync.Update(i_WhenDateTime, i_By, true);
        }

        public virtual void ResolveConflicts(System.DateTime? i_WhenDateTime, string i_By)
        {
            m_Sync.ResolveConflicts(i_WhenDateTime, i_By, true);
        }

        public virtual void Delete(System.DateTime? i_WhenDateTime, string i_By)
        {
            m_Sync.Delete(i_WhenDateTime, i_By, true);
        }

        public virtual Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Sync Sync
        {
            get
            {
                return m_Sync;
            }

            set
            {
                if (m_Sync != null)
                {
                    m_Sync.XElement.Remove();

                    if (!m_XElement.HasElements)
                        m_XElement.Add(value.XElement);
                    else
                        m_XElement.FirstNode.AddBeforeSelf(value.XElement);
                }

                m_Sync = value;
            }
        }

        public Microsoft.CSA.FeedSync.FeedSyncLibrarySL.IFeedSyncItem CreateCopy()
        {
            Microsoft.CSA.FeedSync.FeedSyncLibrarySL.IFeedSyncItem ClonedIFeedSyncItem = this.Clone();

            Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Sync Sync = new Sync
                (
                m_XElement.Document,
                "",
                System.Guid.NewGuid().ToString()
                );

            ClonedIFeedSyncItem.Sync = Sync;

            return ClonedIFeedSyncItem;
        }

        public virtual Microsoft.CSA.FeedSync.FeedSyncLibrarySL.IFeedSyncItem Clone()
        {
            System.Type FeedSyncItemType = this.GetType();
            Microsoft.CSA.FeedSync.FeedSyncLibrarySL.IFeedSyncItem ClonedIFeedSyncItem = (Microsoft.CSA.FeedSync.FeedSyncLibrarySL.IFeedSyncItem)System.Activator.CreateInstance(FeedSyncItemType);

            ClonedIFeedSyncItem.Initialize(new System.Xml.Linq.XElement(m_XElement));

            return ClonedIFeedSyncItem;
        }

        public virtual Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Sync Initialize(System.Xml.Linq.XElement i_XElement)
        {
            if (i_XElement == null)
                throw new System.ArgumentException("i_XElement must be non-null!");

            bool Initialized =
                (m_XElement != null) ||
                (m_Sync != null);

            if (Initialized)
                throw new System.Exception("Already initialized!");

            m_XElement = i_XElement;

            m_Sync = new Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Sync(this);

            return m_Sync;
        }
    }

}
